package component{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.utils.Dictionary;
	
	import utils.ResourceLoader;
	
	public class Image extends Bitmap{
		
		private static var dataDic:Dictionary = new Dictionary();
		
		public var url:String;
		private var complete:Function;
		
		public function Image(){
		}
		
		public function load(url:String,complete:Function=null):void{
			this.url = url;
			this.complete = complete;
			if(dataDic[url] == null){
				ResourceLoader.load(url,loadComplete);
			}else{
				this.bitmapData = dataDic[url];
			}
		}
		
		private function loadComplete(data:Object):void{
			if(dataDic[data.url] == null){
				dataDic[data.url] = data.content;
				this.bitmapData = data.content;
				if(complete != null)complete();
			}
		}
		
		
		public static function getDataFromPool(url:String):BitmapData{
			return dataDic[url];
		}
		
		/**
		 * 批量加载
		 * @dic 文件夹路径
		 * @id dic路径下文件夹名
		 * */
		public static function batchLoad(dicName:String,names:Array,complete:Function=null):void{
			var length:int = names.length;
			while(names.length){
				var name:String = names.pop();
				var url:String = dicName +"/"+ name;
				ResourceLoader.load(url ,function(data:Object):void{
					dataDic[data.url] =  data.content;
					length -- ;
					if(length ==0){
						if(complete != null)complete();
					}
				});
			}
				
		}
		
		
	}
}